package com.mb.app.controller;

import com.mb.app.domain.TbTagCfg;
import com.mb.app.service.ITbTagCfgService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标签配置  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-01
 */
@Controller
@RequestMapping("/app/tagCfg")
@Slf4j
public class TbTagCfgController extends BaseController {
    
    
    @Autowired
    private ITbTagCfgService tbTagCfgService;

    @GetMapping("/index")
    String index(){
        return "/app/tagCfg/tagCfg_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbTagCfg> list = tbTagCfgService.queryList(query);
        int total = tbTagCfgService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "/app/tagCfg/tagCfg_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbTagCfg TbTagCfg, HttpServletRequest request){
        TbTagCfg.setAddUserId(getUserId());
        int result = tbTagCfgService.save(TbTagCfg);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("tagId") Long tagId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = tbTagCfgService.remove(tagId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{tagId}")
    String dtl(@PathVariable("tagId") Long tagId, Model model){
        model.addAttribute("tagCfgInfo",tbTagCfgService.getDtl(tagId));
        return "/app/tagCfg/tagCfg_info";
    }


    @GetMapping("/edit/{tagId}")
    String edit(@PathVariable("tagId") Long tagId, Model model){
        model.addAttribute("tagCfgInfo",tbTagCfgService.getDtl(tagId));
        return "/app/tagCfg/tagCfg_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbTagCfg TbTagCfg, HttpServletRequest request){
        TbTagCfg.setUpdateUserId(getUserId());
        int result = tbTagCfgService.update(TbTagCfg);
        return R.ok();
    }

    /**
     * 物流快递名称快递代码是否存在
     */
    @PostMapping("/tagCfgNameExit")
    @ResponseBody
    boolean tagCfgsNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !tbTagCfgService.tagCfgNameExit(params);
    }





}
